package com.fjrz.bbxwj.mode.api

import com.blankj.utilcode.util.GsonUtils
import com.fjrz.bbxwj.entity.*
import com.module.lemlin.rxhttp.subscribe
import com.fjrz.bbxwj.popup.VideoDiscussPopup
import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.rxhttp.HttpServiceData
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.RxHttp


/**
 * 评论列表
 *
 * @param videoId 视频id
 * @param limit 条数
 * @param page 分页
 * */
fun commentList(
    videoId: String, page: String, limit: String, listener: SubscribeListener<CommentGroupResponse>
): Disposable = subscribe(
    RxHttp.get("/api/comment/list")
        .add("video_id", videoId)
        .add("page", page)
        .add("limit", limit)
        .asClass(CommentGroupResponse::class.java), listener
)

/**
 * 评论列表
 *
 * @param baseCommentId
 * */
fun commentChildList(
    baseCommentId: String, listener: SubscribeListener<CommentChildResponse>
): Disposable = subscribe(
    RxHttp.get("/api/comment/child_list")
        .add("base_comment_id", baseCommentId)
        .asClass(CommentChildResponse::class.java), listener
)

/**
 * 评论点赞
 *
 * @param commentId
 * */
fun commentLike(
    commentId: String, listener: SubscribeListener<CommentGreatResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/comment/like")
        .add("comment_id", commentId)
        .asClass(CommentGreatResponse::class.java), listener
)

/**
 * 评论点赞
 *
 * @param commentId
 * */
fun commentUnLike(
    commentId: String, listener: SubscribeListener<CommentGreatResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/comment/unlike")
        .add("comment_id", commentId)
        .asClass(CommentGreatResponse::class.java), listener
)

/**
 * 评论点赞
 *
 * @param content
 * */
fun commentSubmit(
    content: VideoDiscussPopup.Content, listener: SubscribeListener<CommentSubmitResponse>
): Disposable = subscribe(
    Observable.just(content).flatMap {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(content),
                Map::class.java
            ).filter { it.value != null }
        Observable.just(maps)
    }.flatMap {
        RxHttp.postForm("/api/comment/submit")
            .addAll(it)
            .asClass(CommentSubmitResponse::class.java)
    }, listener
)

/**
 *
 * */
fun commentShare(
    type: String, id: String, listener: SubscribeListener<VideoShareResponse>
): Disposable = subscribe(
    RxHttp.get("/api/common/share")
        .add("type", type)
        .add("id", id)
        .asClass(VideoShareResponse::class.java), listener
)